package com.pj.fantabestconsole.client.widget;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.LabelField;
import com.extjs.gxt.ui.client.widget.layout.HBoxLayout;
import com.extjs.gxt.ui.client.widget.layout.HBoxLayoutData;
import com.extjs.gxt.ui.client.widget.layout.BoxLayout.BoxLayoutPack;
import com.extjs.gxt.ui.client.widget.layout.HBoxLayout.HBoxLayoutAlign;
import com.pj.fantabestconsole.client.ConsoleEvents;
import com.pj.fantabestconsole.client.ConsoleRegistryKeys;

public class LoginPanel extends ContentPanel {

	Button loginButton;
	Button logoffButton;
	LabelField loginLabel;

	public LoginPanel()	{
		this.setHeaderVisible(false);
		this.setBodyBorder(false);
		this.setStyleName("loginPanel");
		HBoxLayout layout = new HBoxLayout();
		layout.setHBoxLayoutAlign(HBoxLayoutAlign.TOP);
		layout.setPack(BoxLayoutPack.END);
		setLayout(layout);
		
		HBoxLayoutData layoutData = new HBoxLayoutData(10, 3, 0, 3);
		loginLabel = new LabelField();
		loginLabel.setVisible(false);
		add(loginLabel,new HBoxLayoutData(3, 3, 3, 3));
		
		loginButton = new Button("Signin");
		loginButton.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
//				LoginWindow loginDialog = new LoginWindow();
//				Registry.register(ConsoleRegistryKeys.LOGIN_DIALOG, loginDialog);
//				loginDialog.show();
			}
		});
		add(loginButton, layoutData);
		
		logoffButton = new Button("Signoff");
		logoffButton.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				Dispatcher.get().dispatch(ConsoleEvents.Logoff);
			}
		});
		logoffButton.setVisible(false);
		add(logoffButton, layoutData);		
	}
	
	public void logManagerIn()	{
		BeanModel manager = Registry.get(ConsoleRegistryKeys.MANAGER_LOGGED_IN);
		loginLabel.setText((String)manager.get("nickname"));
		loginLabel.setVisible(true);
		loginButton.setVisible(false);
		logoffButton.setVisible(true);
		layout();
	}
	
	public void logManagerOff()	{
		loginLabel.setVisible(false);
		loginButton.setVisible(true);
		logoffButton.setVisible(false);
		layout();
	}
	
}
